# _*_ coding : utf-8 _*_
# @Time : 2022/4/20 23:03
# @Author : Wtt
# @File : email
# @Project : fisher
from threading import Thread

from flask import current_app, render_template
from flask_mail import Message

from app import mail


def send_async_mail(app, msg):
    with app.app_context():
        try:
            mail.send(msg)
        except Exception as e:
            pass


def send_email(to, subject, template, **kwargs):
    msg = Message('[鱼书]' + ' ' + subject, sender=current_app.config['MAIL_USERNAME'], recipients=[to])
    msg.html = render_template(template, **kwargs)
    app = current_app._get_current_object()
    thread = Thread(target=send_async_mail, args=[app, msg])
    thread.start()
